﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace Framework.Common
{
    public class CommonMD5
    {
        private CommonMD5()
        { }
        private static readonly CommonMD5 instance = new CommonMD5();
        public static CommonMD5 getInstance()
        {
            return instance;
        }
        /// <summary>
        /// 得到两次MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string Get2MD5(string str)
        {
            return this.MD5(this.MD5(str));
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public String MD5(String str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            String ret = "";
            for (Int32 i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, '0');

            return ret;
        }


        /// <summary>
        /// 上传md5密钥
        /// </summary>
        /// <returns></returns>
        public string GetUploadMD5(DateTime date)
        {

            string datestr = date.ToString("yyyy-MM-dd");
            string password = "";
            byte[] result = Encoding.Default.GetBytes(datestr);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            string mpwd = "";
            password = BitConverter.ToString(output).Replace("-", "").ToUpper();
            mpwd = password.Replace(password.Substring(date.Day, 1), "B");
            return mpwd;

        }
    }
}
